<?php

namespace App\Admin\Controllers;

use App\Models\Message;
use Carbon\Carbon;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Layout\Content;
use Illuminate\Routing\Controller;
use App\Models\HelpWorkOrder;
use Illuminate\Support\Facades\Input;
use Encore\Admin\Layout\Row;

class HelpReplyController extends Controller
{
    use HasResourceActions;

    public function index()
    {
        return redirect(route('help_center.index'));
    }

    public function edit($id, Content $content)
    {
        return $content
            ->header(trans('admin.work_order') . trans('admin.content'))
            ->description('your reply will be send to user and showed in message list')
            ->row(function (Row $row) use ($id) {
                $row->column(4, $this->workOrderForm()->edit($id));
                $row->column(8, $this->form()->edit($id));
            });
    }

    protected function workOrderForm()
    {
        $form = new Form(new HelpWorkOrder());
        $form->setTitle(trans('admin.work_order'));
        $form->display('user.username', trans('admin.username'))->readOnly();
        $form->display('object.no', trans('admin.reprimanded_target'))->readOnly()->with(function ($value) {
            return '<a href="' . route('orders.index', ['no' => $value]) . '">' . $value . '</a>';
        });
        $form->textarea('content', trans('admin.content'))->readOnly();
        $form->display('images', trans('admin.image'))->with(function ($value) {
            return "<a target='_blank' href='" . image_url($value) . "'><img src=" . image_url($value) . " /></a>";
        });
        $form->footer(function (Form\Footer $footer) {
            $footer->disableViewCheck();
            $footer->disableCreatingCheck();
            $footer->disableEditingCheck();
            $footer->disableReset();
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
            $tools->disableList();
        });
        $form->disableSubmit();

        return $form;
    }

    protected function form()
    {
        $form = new Form(new HelpWorkOrder());
        $form->setTitle(trans('admin.reply'));
        $options = [
            'on' => ['value' => 0, 'text' => trans('admin.show'), 'color' => 'success'],
            'off' => ['value' => 1, 'text' => trans('admin.close'), 'color' => 'danger'],
        ];
        $form->switch('is_close', trans('admin.close_work_order'))->states($options)
            ->help('if you close it, It can not be replied anymore !');
        $form->hasMany('reply', trans('admin.work_order_log'), function (Form\NestedForm $reply) use ($form) {
            $reply->textarea('content', trans('admin.reply'))->setElementClass('text-2')
                ->rows(2)->rules(['required'])
                ->setLabelClass(['required']);
//            $reply->image('images', trans('admin.image'));
//            $reply->datetime('created_at', trans('admin.created_at'))->default(Carbon::now());
            $reply->hidden('user_id')->default(Admin::user()->id);
            $reply->hidden('role')->default('admin');

        });

        $form->saved(function ($form) {
            $content = Input::get('reply.new_1.content');

            if ($form->model()->is_close) {
                $content = $content . '(' . trans('admin.work_order_closed') . ')';
            }

            if ($content) {
                $data = [
                    'user_id' => $form->model()->user_id,
                    'title' => trans('admin.work_order_reply'),
                    'content' => $content,
                    'type' => Message::SYS,
                ];

                Message::create($data);
            }
        });

        $form->footer(function (Form\Footer $footer) {
            $footer->disableViewCheck();
            $footer->disableCreatingCheck();
            $footer->disableEditingCheck();
        });
        $form->tools(function (Form\Tools $tools) {
            $tools->disableDelete();
            $tools->disableView();
        });

        return $form;
    }

}
